package com.zhxh.nzpos.query;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.MemberDao;
import com.zhxh.nzpos.model.Member;
import com.zhxh.nzpos.view.CustomDatePicker;

/**
 * 统计查询--订购查询
 * 
 * @author wangzt
 *
 */
public class CardActiveQueryActivity extends BaseActivity {

	private ArrayList<Member> memberItems;
	private MemberListAdapter listAdapter;
	
	private Calendar fromCal = Calendar.getInstance();
	private Calendar toCal = Calendar.getInstance();
	EditText fromDate;
	EditText toDate;
	SimpleDateFormat simpleDateFormat;
	SimpleDateFormat simpleDateFormat2;
	private long total;
	private int pageSize = 5;
	private int currentPage = 1;
	
	MemberDao memberDao;
	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_card_active);
        memberDao = new MemberDao(this);
        initViews();
    }
	
	private void initViews() {
		
		ListView listView = (ListView) findViewById(R.id.buyin_query_listview);
		memberItems = new ArrayList<Member>();
		
		fromDate = (EditText)findViewById(R.id.buyin_from_date);
		toDate = (EditText)findViewById(R.id.buyin_to_date);
		simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		fromDate.setText(simpleDateFormat.format(fromCal.getTime()));
		toDate.setText(simpleDateFormat.format(toCal.getTime()));
        
		final CustomDatePicker fromPicker = new CustomDatePicker(this, fromDate, fromCal);
		fromDate.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				fromPicker.updateDate();
				fromPicker.show();
			}
		});
		
		final CustomDatePicker toPicker = new CustomDatePicker(this, toDate, toCal);
		toDate.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				toPicker.updateDate();
				toPicker.show();
			}
		});

		
		
		listAdapter = new MemberListAdapter(getLayoutInflater(), memberItems);
		listView.setAdapter(listAdapter);
		
		ImageView cancelBtn = (ImageView) findViewById(R.id.buyin_query_close_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	
	public void search(View view) {
		// finish();
		currentPage = 1;
		doSearch(view);
		doGetCount(view);

	}

	public void page_next(View view) {
		
		if (currentPage * pageSize <= total)
		{
			currentPage++;
			doSearch(view);
		}
			
		
	}

	public void page_previous(View view) {

		currentPage--;

		if (currentPage < 1) {
			currentPage = 1;
		}

		doSearch(view);

	}

	public void doSearch(View view) {

		Date btime;
		Date etime;
		try {
			btime = simpleDateFormat.parse(fromDate.getText().toString());
			etime = simpleDateFormat2.parse(toDate.getText().toString()+" 23:59:59");
			
			 ArrayList<Member> items = memberDao.getScrollData(btime.getTime(), etime.getTime(), currentPage, pageSize);
			 memberItems.clear();
			 memberItems.addAll(items);
		
			if (listAdapter != null) {
				
				listAdapter.notifyDataSetChanged();
			}

		} catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void doGetCount(View view) {
        
		Date btime;
		Date etime;
		try {
			btime = simpleDateFormat.parse(fromDate.getText().toString());
			etime = simpleDateFormat2.parse(toDate.getText().toString()+" 23:59:59");

			total = memberDao.getCount( btime.getTime(),
					etime.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
